package ui.panels;

import entities.Holiday;
import java.awt.BorderLayout;
import java.awt.Color;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import manager.GenericDriver;
import manager.ManagerDriver;
import model.EditHolidayTableModel;
import ui.windows.RemedySchedulerFrame;

public class EditHolidayPanel extends javax.swing.JPanel {

    /** Creates new form EditHolidayPanel */
    public EditHolidayPanel(RemedySchedulerFrame mainFrame) {
        this.mainFrame = mainFrame;
        driver = (ManagerDriver) mainFrame.getDriver();
        genDriver = mainFrame.getGenericDriver();
        initComponents();
        includeModel();
    }

    private void includeModel() {
        holidayModel = new EditHolidayTableModel(genDriver.showHoliday());
        holidayTable.setModel(holidayModel);
        holidayTable.setFillsViewportHeight(true);
        holidayTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        holidayTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        DefaultTableCellRenderer cellRenderer = new DefaultTableCellRenderer();
        cellRenderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        cellRenderer.setBackground(Color.yellow);
        DefaultTableCellRenderer cellRenderer1 = new DefaultTableCellRenderer();
        cellRenderer1.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        cellRenderer1.setBackground(new Color(104,255,104));
        holidayTable.getColumnModel().getColumn(0).setPreferredWidth(170);
        holidayTable.getColumnModel().getColumn(0).setCellRenderer(cellRenderer);
        holidayTable.getColumnModel().getColumn(1).setPreferredWidth(100);
        holidayTable.getColumnModel().getColumn(1).setCellRenderer(cellRenderer1);
        holidayTable.getColumnModel().getColumn(2).setPreferredWidth(100);
        holidayTable.getColumnModel().getColumn(2).setCellRenderer(cellRenderer1);
        holidayTable.getColumnModel().getColumn(3).setPreferredWidth(300);
        holidayTable.getColumnModel().getColumn(4).setPreferredWidth(100);

        createHolidayPanel = new CreateHolidayPanel(mainFrame, this);

        lbCheck.setText("");
        target = 1;
    }

    private void setSelectedHoliday() {
        int index = holidayTable.getSelectedRow();
        if (index != -1) {
            selectedHoliday = holidayModel.getAt(index);
            txtTitle.setText(selectedHoliday.getTitle());
            dcStartDate.setDate(selectedHoliday.getStartDate());
            dcEndDate.setDate(selectedHoliday.getEndDate());
            txaDescription.setText(selectedHoliday.getDescription());
        } else {            
            selectedHoliday = null;
            txtTitle.setText("");
            dcStartDate.setDate(null);
            dcEndDate.setDate(null);
            txaDescription.setText("");
        }
    }

    private void swithAction() {
        int choise = cbActions.getSelectedIndex();
        switchTarget(choise);
        cbActions.setSelectedIndex(0);
    }

    private void switchTarget(int choise) {
        if (target != choise || target == 4) {
            switch (choise) {
                case 0:
                    break;
                case 1:
                    this.remove(createHolidayPanel);
                    target = 1;
                    this.add(editHoliday, BorderLayout.CENTER);
                    this.revalidate();
                    break;
                case 2:
                    this.remove(editHoliday);
                    target = 2;
                    this.add(createHolidayPanel, BorderLayout.CENTER);
                    this.revalidate();
                    break;
                case 3:
                    new Thread() {

                        @Override
                        public void run() {
                            updateHoliday();
                        }
                    }.start();
                    break;
                case 4:
                    new Thread() {

                        @Override
                        public void run() {
                            deleteHoliday();
                        }
                    }.start();
                    break;
            }
        }
    }

    public boolean createHoliday(Holiday holiday) {
        boolean returnVal = driver.insertHoliday(holiday);
        if (returnVal) {
            holiday.setModifiedDate(Calendar.getInstance().getTime());
            holidayModel.addElement(holiday);
        }
        return returnVal;
    }

    private void updateHoliday() {
        if (selectedHoliday != null) {
            Date startDate = dcStartDate.getDate();
            Date endDate = dcEndDate.getDate();
            String description = txaDescription.getText();
            if (startDate != null) {
                if (endDate != null) {
                    if (description != null && description.length() != 0) {
                        if (startDate.before(endDate)) {
                            selectedHoliday.setTitle(txtTitle.getText());
                            selectedHoliday.setStartDate(startDate);
                            selectedHoliday.setEndDate(endDate);
                            selectedHoliday.setDescription(description);
                            boolean returnVal = driver.updateHoliday(selectedHoliday);
                            if(returnVal) {
                                holidayModel.update(selectedHoliday);
                                clear();
                                JOptionPane.showMessageDialog(null, "Updated");
                            } else {
                                JOptionPane.showMessageDialog(null, "Cannot update", "Error", JOptionPane.WARNING_MESSAGE);
                            }
                        } else {
                            lbCheck.setText("date invalid !");
                            lbCheck.setForeground(Color.red);
                        }
                    } else {
                        lbCheck.setText("enter description...");
                        lbCheck.setForeground(Color.blue);
                    }
                } else {
                    lbCheck.setText("enter enddate...");
                    lbCheck.setForeground(Color.blue);
                }
            } else {
                lbCheck.setText("enter startdate...");
                lbCheck.setForeground(Color.blue);
            }
        } else {
            JOptionPane.showMessageDialog(null, "You migh select one holiday");
        }
    }

    private void deleteHoliday() {
        if (selectedHoliday != null) {
            int choise = JOptionPane.showConfirmDialog(null, "You sure you want to delete this holiday ?", "message", JOptionPane.YES_NO_OPTION);
            if (choise == JOptionPane.YES_OPTION) {
                boolean returnVal = driver.deleteHoliday(selectedHoliday);
                if (returnVal) {
                    holidayModel.remove(selectedHoliday);
                    clear();
                    JOptionPane.showMessageDialog(null, "Deleted");
                } else {
                    JOptionPane.showMessageDialog(null, "Cannot delete", "Error", JOptionPane.WARNING_MESSAGE);
                }
            }
        } else {
            JOptionPane.showMessageDialog(null, "You migh select one holiday");
        }
    }

    public void showAllHoliday() {
        switchTarget(1);
    }

    public void showCreateHoliday() {
        switchTarget(2);
    }

    public void clear() {
        txtTitle.setText("");
        selectedHoliday = null;
        dcStartDate.setDate(null);
        dcEndDate.setDate(null);
        txaDescription.setText("");
        lbCheck.setText("");
    }

    public void closeTarget() {
        switchTarget(1);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private RemedySchedulerFrame mainFrame;
    private CreateHolidayPanel createHolidayPanel;
    private GenericDriver genDriver;
    private ManagerDriver driver;
    private EditHolidayTableModel holidayModel;
    private Holiday selectedHoliday;
    private int target;
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        cbActions = new javax.swing.JComboBox();
        editHoliday = new javax.swing.JSplitPane();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        dcStartDate = new com.toedter.calendar.JDateChooser();
        jLabel2 = new javax.swing.JLabel();
        dcEndDate = new com.toedter.calendar.JDateChooser();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txaDescription = new javax.swing.JTextArea();
        lbCheck = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtTitle = new javax.swing.JTextField();
        viewControl = new javax.swing.JScrollPane();
        holidayTable = new javax.swing.JTable();

        setBackground(new java.awt.Color(255, 255, 255));
        setLayout(new java.awt.BorderLayout());

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        cbActions.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Actions", "View", "Add", "Update", "Delete" }));
        cbActions.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbActionsActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(445, Short.MAX_VALUE)
                .addComponent(cbActions, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cbActions, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        add(jPanel1, java.awt.BorderLayout.PAGE_START);

        editHoliday.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setText("StartDate:");

        jLabel2.setText("EndDate:");

        jLabel3.setText("Description:");

        txaDescription.setColumns(20);
        txaDescription.setRows(5);
        jScrollPane2.setViewportView(txaDescription);

        lbCheck.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbCheck.setText("value");

        jLabel4.setText("Title:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel1))
                        .addGap(10, 10, 10)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(dcStartDate, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(dcEndDate, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(txtTitle)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 530, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lbCheck, javax.swing.GroupLayout.PREFERRED_SIZE, 307, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(35, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(dcStartDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(dcEndDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbCheck)
                .addContainerGap())
        );

        editHoliday.setTopComponent(jPanel2);

        holidayTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        holidayTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                holidayTableMouseClicked(evt);
            }
        });
        viewControl.setViewportView(holidayTable);

        editHoliday.setRightComponent(viewControl);

        add(editHoliday, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

private void holidayTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_holidayTableMouseClicked
    setSelectedHoliday();
}//GEN-LAST:event_holidayTableMouseClicked

private void cbActionsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbActionsActionPerformed
    swithAction();
}//GEN-LAST:event_cbActionsActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbActions;
    private com.toedter.calendar.JDateChooser dcEndDate;
    private com.toedter.calendar.JDateChooser dcStartDate;
    private javax.swing.JSplitPane editHoliday;
    private javax.swing.JTable holidayTable;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lbCheck;
    private javax.swing.JTextArea txaDescription;
    private javax.swing.JTextField txtTitle;
    private javax.swing.JScrollPane viewControl;
    // End of variables declaration//GEN-END:variables
}
